package com.bolehui.base.result;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 统一返回值
 * @ Author     ：linwanghui
 * @ Date       ：Created in  2020/9/4
 * @Email ：linwanghui@gz.iscas.ac.cn
 */

public class Result<T> implements Serializable {

    private String code;

    private String message;
    //文本展示信息
    private String text;
    //表格底部文本区
    private String bootomText;


    private T data;

    @Getter
    @Setter
    private Integer errcode;

    @Getter
    @Setter
    private String errmsg;


    public Result() {
    }

    private void setErrcode() {
        this.errcode = Integer.valueOf(this.code);
        this.errmsg = this.message;
    }

    public Result(boolean success) {
        this.code = success ? ResultCode.SUCCESS.getCode() : ResultCode.FAIL.getCode();
        this.message = success ? ResultCode.SUCCESS.getMessage() : ResultCode.FAIL.getMessage();

        setErrcode();
    }

    public Result(boolean success, ResultCode resultEnum) {
        this.code = success ? ResultCode.SUCCESS.getCode() : (resultEnum == null ? ResultCode.FAIL.getCode() : resultEnum.getCode());
        this.message = success ? ResultCode.SUCCESS.getMessage() : (resultEnum == null ? ResultCode.FAIL.getMessage() : resultEnum.getMessage());

        setErrcode();
    }

    public Result(boolean success, T data) {
        this.code = success ? ResultCode.SUCCESS.getCode() : ResultCode.FAIL.getCode();
        this.message = success ? ResultCode.SUCCESS.getMessage() : ResultCode.FAIL.getMessage();
        this.data = data;

        setErrcode();
    }

    public Result(boolean success, ResultCode resultEnum, T data) {
        this.code = success ? ResultCode.SUCCESS.getCode() : (resultEnum == null ? ResultCode.FAIL.getCode() : resultEnum.getCode());
        this.message = success ? ResultCode.SUCCESS.getMessage() : (resultEnum == null ? ResultCode.FAIL.getMessage() : resultEnum.getMessage());
        this.data = data;

        setErrcode();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage("success");
        result.setData(data);
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(message);
        result.setData(data);
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage("success");
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> fail(String code, String message) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMessage(message);
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.FAIL.getCode());
        result.setMessage(message);
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        Result<T> result = new Result<T>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> fail(String message,T data) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.FAIL.getCode());
        result.setData(data);
        result.setMessage(message);
        result.setErrcode(Integer.valueOf(result.getCode()));
        result.setErrmsg(result.getMessage());
        return result;
    }

    public static <T> Result<T> fail(String code, String message, T data) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setErrcode(-1);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> boolean isSuccess(Result<T> result) {
        if (result == null) {
            return false;
        }
        if (result.getCode() != null && result.getCode().equals(ResultCode.SUCCESS.getCode() )) {
            return true;
        } else {
            return false;
        }
    }

    public static <T> boolean hasData(Result<T> result) {
        if (isSuccess(result)) {
            if(result.getData() != null){
                return true;
            }
        }
        return false;
    }

    public String getBootomText() {
        return bootomText;
    }

    public void setBootomText(String bootomText) {
        this.bootomText = bootomText;
    }
}